package com.lgt_enterprise_android.view.pages.b.workors;

import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.lgt_enterprise_android.R;
import com.lgt_enterprise_android.adapter.MyWorkorsAdapter;
import com.lgt_enterprise_android.base.BaseActivity;
import com.lgt_enterprise_android.utils.BasicHttp;
import com.lgt_enterprise_android.utils.HttpManager;
import com.swipemenulistview.SwipeMenu;
import com.swipemenulistview.SwipeMenuCreator;
import com.swipemenulistview.SwipeMenuItem;
import com.swipemenulistview.SwipeMenuListView;
import com.util.AppUtil;

public class MyWorkersActivity extends BaseActivity {

    private SwipeMenuListView mSwipeMenuListView;
    private MyWorkorsAdapter adapter;

    private JSONArray mJsonArray = new JSONArray();

    private int pageNumber = 1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_workers);

        initView();
        getData();
    }

    private void initView() {
        mSwipeMenuListView = (SwipeMenuListView) findViewById(R.id.swipeMenuListView);

        mSwipeMenuListView.setOnRefreshListener(new SwipeMenuListView.OnRefreshListener() {
            @Override
            public void onRefresh() {
                pageNumber = 1;
                getData();
            }
        });

        mSwipeMenuListView.setOnLoadListener(new SwipeMenuListView.OnLoadListener() {
            @Override
            public void onLoad() {
                pageNumber++;
                getData();
                mSwipeMenuListView.onLoadComplete();
            }
        });

        mSwipeMenuListView.isShowIsload(false);
        mSwipeMenuListView.setMenuCreator(new SwipeMenuCreator() {
            @Override
            public void create(SwipeMenu menu) {
                SwipeMenuItem item = new SwipeMenuItem(mContext);
                item.setBackground(new ColorDrawable(Color.rgb(0xf4, 0x50, 0x2c)));
                item.setWidth(110 * AppUtil.getdensityIntDpi(mContext));
                item.setTitle("离职");
                item.setTitleSize(12);
                item.setTitleColor(Color.WHITE);
                menu.addMenuItem(item);

            }
        });

        mSwipeMenuListView.setOnMenuItemClickListener(new SwipeMenuListView.OnMenuItemClickListener() {
            @Override
            public void onMenuItemClick(int position, SwipeMenu menu, int index) {
                HttpManager.workerLeft(mContext, adapter.getStaffIdByPos(position), new BasicHttp.IMyCallBack() {
                    @Override
                    public void onResponse(Object resonseObj) {
                        mSwipeMenuListView.onRefresh();
                        getData();
                    }

                    @Override
                    public void onFailure(String mes) {

                    }
                });
            }
        });

        mSwipeMenuListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int pos, long l) {
                if (pos == 0) {
                    return;
                }

                Intent intent = new Intent(mContext, WorkerDetailActivity.class);
                intent.putExtra("staff_id", adapter.getStaffIdByPos(pos - 1));
                startActivity(intent);
            }
        });


        adapter = new MyWorkorsAdapter(mContext, mJsonArray);
        mSwipeMenuListView.setAdapter(adapter);
    }


    private void getData() {
        HttpManager.getWorkerList(mContext, pageNumber,15, new BasicHttp.IMyCallBack() {
            @Override
            public void onResponse(Object resonseObj) {
                JSONArray jsonArray = ((JSONObject) resonseObj).getJSONArray("list");
                if (pageNumber == 1) {
                    adapter.setData(jsonArray);
                    mSwipeMenuListView.onRefreshComplete();
                } else {
                    adapter.addData(jsonArray);
                    mSwipeMenuListView.onLoadComplete();
                }
                mSwipeMenuListView.setResultSize(jsonArray.size());
                adapter.notifyDataSetChanged();
            }

            @Override
            public void onFailure(String mes) {

            }
        });

    }
}
